﻿using System;
using System.Collections.Generic;
using System.Text;

using Torch.WL.Entity.CleanChina;
using System.Data.SqlClient;
using System.Data;

namespace Torch.WL.DataAccess.CleanChina
{
    public class TbWeiboAccess
    {
        public TbWeiboAccess()
        {

        }


        private TbWeiboEntity DataRowToModel(DataRow row)
        {
            TbWeiboEntity model = new TbWeiboEntity();
            if (row != null)
            {
                if (row["wbId"] != null && row["wbId"].ToString() != "")
                {
                    model.wbId = int.Parse(row["wbId"].ToString());
                }
                if (row["weibo"] != null)
                {
                    model.weibo = row["weibo"].ToString();
                }
                if (row["weibo_images"] != null)
                {
                    string[] arrStr = row["weibo_images"].ToString().Split(',');
                    List<string> list = new List<string>(arrStr);
                    model.weibo_images = list;
                }
                if (row["submitTime"] != null && row["submitTime"].ToString() != "")
                {
                    model.submitTime = DateTime.Parse(row["submitTime"].ToString());
                }
                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
            }
            return model;
        }

        public List<TbWeiboEntity> GetWeiboList(int lessthan_wbid, int pagesize)
        {
            //select top 0  * FROM dbo.Tb_Weibo where submitTime<'' order by submitTime desc
            if (lessthan_wbid<=0)
                lessthan_wbid = Int32.MaxValue;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top " + pagesize + " * from Tb_Weibo ");
            strSql.Append(" where wbid<@lessthan_wbid order by wbid desc");
            SqlParameter[] parameters = {
					new SqlParameter("@lessthan_wbid", lessthan_wbid)
			};

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return DataTableToList(ds.Tables[0]);
        }

        private List<TbWeiboEntity> DataTableToList(DataTable dt)
        {
            List<TbWeiboEntity> modelList = new List<TbWeiboEntity>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TbWeiboEntity model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

    }
}
